<template>
    <div>
        <el-table :data="tableData" border style="width: 100%;" ref="multipleTable"
            @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column fixed prop="date" label="日期" width="150" sortable>
            </el-table-column>
            <el-table-column prop="name" label="姓名" width="150">
            </el-table-column>
            <el-table-column prop="province" label="省份" width="100">
            </el-table-column>
            <el-table-column prop="city" label="市区" width="100">
            </el-table-column>
            <el-table-column prop="address" label="地址" width="300">
            </el-table-column>
            <el-table-column prop="zip" label="邮编" width="100">
            </el-table-column>
            <el-table-column prop="status" label="状态" width="80">
                <template slot-scope="scope">
                    <el-tag type="success" v-show="scope.row.status == 1">正常</el-tag>
                    <el-tag type="danger" v-show="scope.row.status == 2">禁用</el-tag>
                </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="140">
                <template slot-scope="scope">
                    <el-button @click="handleClick(scope.row)" type="text" size="small">查看</el-button>
                    <el-button type="text" size="small">编辑</el-button>
                    <el-button type="text" size="small" @click="showDelete(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-dialog title="删除" :visible.sync="deleteVisible" width="30%">
            <span>确定删除？操作不可恢复</span>
            <span slot="footer" class="dialog-footer">
                <el-button @click="deleteVisible = false">取 消</el-button>
                <el-button type="danger" @click="subDelete">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    name: "MyTable",
    props: [],
    data() {
        return {
            // 展示删除页面
            deleteVisible: false,
            // 删除的信息
            deleteInfo: {},
        }
    },
    methods: {
        // 多选
        handleSelectionChange(val) {
            this.multipleSelection = val
        },
        // 查看
        handleClick(data) {
            console.log(data)
        },
        // 展示删除
        showDelete(data){
            this.deleteInfo = data
            this.deleteVisible = true
        },
        // 删除
        async subDelete(){
            let res = await this.$store.dispatch('deleteTableInfo', this.deleteInfo.id)
            if(res.success){
                // 重新渲染数据
                this.$emit('getTableData')
                this.$message.success(res.message)
            }
            else this.$message.error(res.message)
        }
    },
    computed: {
        tableData() {
            return this.$store.state.table.tableData
        }
    }
}
</script>

<style scope></style>